﻿Imports System.Data

Partial Class str_purchreq
    Inherits System.Web.UI.Page
#Region "SQL Commands"
    Function CheckItem() As Boolean
        Dim dv As DataView = Nothing
        Try
            dv = CType(sqlParentCheck.Select(DataSourceSelectArguments.Empty), DataView)
            If dv.Count < 1 Then
                Return False
            Else
                Return True
            End If
        Catch ex As Exception
            WriteParentError(ex.ToString, Drawing.Color.Red, lblParentReturn)
            Return False
        End Try
    End Function
    Function InsertParent() As Boolean
        Try
            SqlDataSource1.Insert()
            WriteParentError("Saved!", Drawing.Color.Black, lblParentReturn)

            Return True
        Catch ex As Exception
            WriteParentError(ex.Message, Drawing.Color.Red, lblParentReturn)

            Return False
        End Try
    End Function
    Sub canceltxn()
        Try
            SqlDataSource1.Delete()
            sqlItem.Delete()
            Response.Redirect(Request.Url.AbsoluteUri)
        Catch ex As Exception
            WriteParentError(ex.Message, Drawing.Color.Black, lblParentReturn)
        End Try
    End Sub
#End Region
#Region "Misc"
    Sub WriteParentError(statement As String, ForeColor As Drawing.Color, labeltarget As Label, Optional focusoncontrol As Control = Nothing)
        Try
            If labeltarget Is lblParentReturn Then
                If ForeColor = Drawing.Color.Red Then
                    labeltarget.CssClass = "ErrorLogs"
                Else
                    lblParentReturn.CssClass = "SuccessLogs"
                End If
                ForeColor = Drawing.Color.Black
            End If
            focusoncontrol.Focus()
        Catch ex As Exception

        End Try
        labeltarget.ForeColor = ForeColor
        labeltarget.Text = statement
    End Sub

    Sub ClearItemReturn()
        lblqty.Text = ""
        lblitemdesc.Text = ""
    End Sub
#End Region

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        'clApprovd.SelectedDate = Now.Date
        'clApprovedPO.SelectedDate = Now.Date
        'clcanvassed.SelectedDate = Now.Date
        'clPrepared.SelectedDate = Now.Date
        'clRecommend.SelectedDate = Now.Date
    End Sub

    Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If (txtPR.Text = "") Then
            Return
        End If
        If (txtRequisitioner.Text = "") Then
            Return
        End If
        If (txtprepred.Text = "") Then
            Return
        End If
        If (txtApproved.Text = "") Then
            Return
        End If
        If (txtPOApproved.Text = "") Then
            Return
        End If

        Try
            If (rdKR.Checked) Then
                Session("sel_company") = rdKR.Text
            ElseIf (rdSB.Checked) Then
                Session("sel_company") = rdSB.Text
            ElseIf (rdKG.Checked) Then
                Session("sel_company") = rdKG.Text
            End If
            If (rdStd.Checked) Then
                Session("PR_Type") = rdStd.Text
            ElseIf (rdSmallwares.Checked) Then
                Session("PR_Type") = rdSmallwares.Text
            ElseIf (rdFixed.Checked) Then
                Session("PR_Type") = rdFixed.Text
            End If

            If (rbHigh.Checked) Then
                Session("Priority") = rbHigh.Text
            ElseIf rbNorm.Checked Then
                Session("Priority") = rbNorm.Text
            Else
                Session("Priority") = ""
            End If


            Session("time_stamp") = Now.ToString
            Session("agent") = User.Identity.Name.ToString

            If (CheckItem() = True) Then
                WriteParentError("PR NO. Already Exist!", Drawing.Color.Red, lblParentReturn, txtPOApproved)
                Return
            End If


            If InsertParent() = True Then
                WriteParentError("Saved!", Drawing.Color.Black, lblParentReturn, txtPOApproved)

                prentpanel.Enabled = False
                itemPanel.Enabled = True
                Button5.Enabled = False
                Button4.Enabled = True
            End If
        Catch ex As Exception
            WriteParentError(ex.Message, Drawing.Color.Red, lblParentReturn, txtPOApproved)
        End Try
    End Sub

    

    Protected Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        If txtqty.Text = "" Then
            WriteParentError("Quantity must be specify!", Drawing.Color.Red, lblqty, txtqty)
            Return
        End If
        If txtdescription.Text = "" Then
            WriteParentError("Description must be specify!", Drawing.Color.Red, lblitemdesc, txtdescription)
            Return
        End If

        Try
            sqlItem.Insert()
            txtqty.Text = ""
            txtdescription.Text = ""
            txtremarks.Text = ""
            Button5.Enabled = True
        Catch ex As Exception
            WriteParentError(ex.Message, Drawing.Color.Red, lblitemdesc0)
            Return
        End Try

    End Sub

   

    Protected Sub txtqty_TextChanged(sender As Object, e As EventArgs) Handles txtqty.TextChanged
        ClearItemReturn()
    End Sub

    Protected Sub txtdescription_TextChanged(sender As Object, e As EventArgs) Handles txtdescription.TextChanged
        ClearItemReturn()
    End Sub

    Protected Sub txtremarks_TextChanged(sender As Object, e As EventArgs) Handles txtremarks.TextChanged
        ClearItemReturn()
    End Sub

    Protected Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        Response.Redirect(Request.Url.AbsoluteUri)
    End Sub

    Protected Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Try
            canceltxn()
        Catch ex As Exception
        End Try
    End Sub
End Class
